﻿/**
 * 前提逻辑
 */
(function($){
	// 构造器
	$.fn.logicPremise = function(){
		$.fn.logicPremise.prototype = $.fn.logicPremise.__proto__ = $.fn.logicPremise.superO = $.fn.logicComponent;
		return $.fn.logicPremise;
	};
	
	// 初始化
	$.fn.logicPremise.init = function(data){
	};
	
	// 创建逻辑对象
	$.fn.logicPremise.newLogic = function(logictype){
		// 主逻辑
		var logicmaster = this.superO.newLogic();
		var data = LogicParams.data; // 问卷内容
		var survey = data.survey; // 问卷信息
		var masterQuestion = data.questionlist[1]; // 主问题为 问题列表第2题

		logicmaster.masteruuid = new Date().getTime(); // 主逻辑uuid
		
		// 详细逻辑
		var logicdetails = new Array();
		var detailLogic = {};
		var detailQuestion = data.questionlist[0];; // 问题
		detailLogic.detailuuid = new Date().getTime();
		detailLogic.logicuuid = logicmaster.masteruuid;
		detailLogic.condition = 1; // 选中
		detailLogic.fatherquestionuuid = detailQuestion.fatherquestionuuid;
		detailLogic.questionuuid = detailQuestion.questionuuid;
		detailLogic.answeruuid = detailQuestion.answers[0].answeruuid;
		detailLogic.sortindex = 1;
		logicdetails[0] = detailLogic;
		
		// 逻辑基本属性
		logicmaster.surveyuuid = survey.surveyuuid;
		logicmaster.questionuuid = masterQuestion.questionuuid;
		logicmaster.fatherquestionuuid = masterQuestion.fatherquestionuuid;
		logicmaster.answeruuid = "";
		logicmaster.type = logictype;
		logicmaster.sortindex = LogicParams.logiclist.length + 1; // 存储的过程中将数据存入logicList
		logicmaster.logicdetails = logicdetails;
		logicmaster.pageuuid = masterQuestion.pageuuid;
		
		logicmaster.masterQuestion = masterQuestion;
		logicmaster.detailQuestion = detailQuestion;
		// masteruuid pageuuid type
		// fatherquestionuuid questionuuid answeruuid  createtime sortindex
		
		// 获取详细问题条件
		// 问题的选项
		// 其他问题
		// -- 1 点击问题框可以切换答案 
		// -- 2 只能获取比自己题号大的题
		// -- 3 条件集合
		return logicmaster;
	};
	
   /** 
	* 显示逻辑
	*/
	$.fn.logicPremise.show = function(logicmaster){
		var masteruuid = logicmaster.masteruuid;
		var sortindex = logicmaster.sortindex;
		var detailLogic = logicmaster.logicdetails[0]; //暂定只支持一种前提
		
		var html = "";
		html += "<div id=\"logicmasterdiv_" + masteruuid + "\" class=\"logic-expression-box logic-item-warp\">";
		html += 	"<div class=\"num fl mt8\">";	// 序号
		html +=  		"<a class=\"index\" id=\"logicmaster_no_" + masteruuid + "\">"+sortindex+"</a>";
		html += 	"</div>";
		html +=		"<div class=\"logic-expression-item logic-item-rt\">";	// 问题详情
		html += 		"<div id=\"logicdetaildiv_" + logicmaster.masteruuid + "_" + detailLogic.detailuuid + "\" class=\"logic-expression\">";
		html += 			this.getDetailOneInnerHtml(logicmaster);
		html += 		"</div>";
		html += 		"<div id=\"logicmasterinnerdiv_" + logicmaster.masteruuid + "\" class=\"logic-expression logic-result\">";
		html += 			this.getMasterQuestionHtml(logicmaster);
		html += 		"</div>";
		html += 	"</div>";
		html += 	"<div class=\"clear\"></div>";
		html +=		"<div class=\"logicActions-box\">";	// 操作栏
		html += 		"<div class=\"logicActions\">";
		/*html +=				"<a href=\"javascript:void(0);\" onclick=\"copyLogic('" + masteruuid + "');return false;\" title=\"复制当前逻辑\">";
		html +=					"<img alt=\"复制当前逻辑\" src=\"" + logicImgPath + "copy_16.gif\" class=\"faDup\" />复制逻辑";
		html +=  			"</a>";
		html +=				" ";*/
		html += 			"<a href=\"javascript:void(0);\" onclick=\"removeLogic('" + masteruuid + "');return false;\" title=\"删除当前逻辑\">";
		html +=					"<img src=\"" + logicImgPath + "delete.png\" class=\"faDel\" />删除逻辑";
		html += 			"</a>";
		html += 		"</div>";
		html += 		"<div class=\"add-logic\" id=\"masteradd_" + masteruuid + "_2\">";
		html +=				"<a href=\"javascript:void(0);\" onclick=\"return false;\" title=\"创建选项逻辑\">";
		html +=					"<img width=\"16\" height=\"16\" src=\"" + logicImgPath + "add.png\" />选项逻辑";
		html +=				"</a>";
		html +=			"</div>";
		html +=			"<div class=\"add-logic\" id=\"masteradd_" + masteruuid + "_1\">";
		html +=				"<a href=\"javascript:void(0);\" onclick=\"return false;\" title=\"创建题逻辑\">";
		html +=					"<img width=\"16\" height=\"16\" src=\"" + logicImgPath + "add.png\" />题逻辑";
		html +=				"</a>";
		html +=			"</div>";
		html += 	"</div>";
		html += 	"<div class=\"clear\"></div>";
		html += "</div>";
		$('#logicDIVEdit').append(html);
	};
	
   /** 
	* 详细逻辑
	*/
	$.fn.logicPremise.getDetailOneInnerHtml = function(logicmaster){
		// 1 逻辑创建详细问题
		var detailLogic = logicmaster.logicdetails[0]; //暂定只支持一种前提
		var detailQuestion = logicmaster.detailQuestion;
		var detailDivTxt = LogicTool.getDivTxt(LogicParams.QUESTION_PREFIX + detailQuestion.sortindex + ".",detailQuestion.description); // 问题显示
		var detailDivTitle = LogicTool.getDivTxtTitle(detailQuestion.description); // 显示提示
		var detailAnswer = LogicTool.getDivTxt("",LogicTool.removeALLHTML(LogicParams.getAnswerByAnsweruuid(detailQuestion.answers,detailLogic.answeruuid).name));
		var detailAnswerTitle = LogicTool.getDivTxtTitle(detailAnswer); // 显示提示
		var condtionTxt = LogicParams.codtionMap[detailLogic.condition].name;
		
		// 2 逻辑问题调整
		detailLogic.fatherquestionuuid = detailQuestion.fatherquestionuuid;
		detailLogic.questionuuid = detailQuestion.questionuuid;
		
		// 3 显示页面
		var html = "";
		html += 	"<div class=\"l-selectors\">";
		html += 		"<span id=\"logicdetail_andordiv_" + logicmaster.masteruuid + "_" + detailLogic.detailuuid + "\">";
		html +=				"<span class=\"expressionfile\">当问题</span>";
		html +=			"</span>";
		html += 		"<a href=\"javascript:void(0);\" onclick=\"return false;\" id=\"logicdetail_qid_" + logicmaster.masteruuid + "_" + detailLogic.detailuuid + "\" title=\"" + detailDivTitle + "\" class=\"menubutton\">";
		html +=				"<b><b><b>" + detailDivTxt + "</b></b></b>";
		html += 		"</a>";
		html += 		"<a href=\"javascript:void(0);\" onclick=\"return false;\" id=\"logicdetail_condition_" + logicmaster.masteruuid + "_" + detailLogic.detailuuid + "\" class=\"menubutton\">";
		html +=				"<b><b><b>" + condtionTxt + "</b></b></b>";
		html +=			"</a>";
		html +=			"<span class=\"expressionfile\">的选项为</span>";
		html +=			"<a href=\"javascript:void(0);\" onclick=\"return false;\" id=\"logicdetail_answer_" + logicmaster.masteruuid + "_" + detailLogic.detailuuid + "\" title=\"" + detailAnswerTitle + "\"  class=\"menubutton\">";
		html +=				"<b><b><b>" + detailAnswer + "</b></b></b>"; 
		html += 		"</a>";
		html +=			"<span class=\"expressionfile\">的时候，</span>";
		html += 	"</div>";
		/*html +=		"<div class=\"l-buttons\">";
		html +=			"<a id=\"logicdetail_add_d4362157-2814-411e-9725-36862056d956\" href=\"javascript:void(0);\" onclick=\"insertDetail('7a5e3f21-f4e5-4b23-a970-2c610e5d1c07','d4362157-2814-411e-9725-36862056d956');return false;\" title=\"增加条件\">";
		html +=				"<img src=\""+ logicImgPath +"add.png\" height=\"16\" width=\"16\">";
		html +=			"</a>";
		html +=			"<a class=\"disabled\" id=\"logicdetail_remove_d4362157-2814-411e-9725-36862056d956\" href=\"javascript:void(0);\" onclick=\"removeDetail('7a5e3f21-f4e5-4b23-a970-2c610e5d1c07','d4362157-2814-411e-9725-36862056d956');return false;\" title=\"删除条件\">";
		html +=				"<img src=\""+logicImgPath+"delete.png\" height=\"16\" width=\"16\">";
		html +=			"</a>";
		html +=		"</div>";*/
		html +=		"<div class=\"clear\"></div>";
		return html;
	};
	
   /** 
	* 主问题HTML形成
	*/
	$.fn.logicPremise.getMasterQuestionHtml = function(logicmaster){
		// 1 创建主问题
		var masterQuestion = logicmaster.masterQuestion;
		var masterDivTxt = LogicTool.getDivTxt(LogicParams.QUESTION_PREFIX + masterQuestion.sortindex + ".",masterQuestion.description);
		
		// 2 设置主问题
		logicmaster.questionuuid = masterQuestion.questionuuid;
		logicmaster.fatherquestionuuid = masterQuestion.fatherquestionuuid;
		
		var html = "";
		html +=		"<div class=\"l-selectors\">";
		html += 		"<span class=\"expressionfile\">则跳问</span>";
		html +=				"<a href=\"javascript:void(0);\" onclick=\"return false;\" id=\"logicmaster_qid_" + logicmaster.masteruuid + "\" class=\"menubutton\">";
		html +=					"<b><b><b>" + masterDivTxt + "</b></b></b>";
		html +=				"</a>";
		html +=		"</div>";
		html +=		"<div class=\"clear\"></div>";
		return html;
	}
})(jQuery);